﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL;
using System.Web.UI.HtmlControls;
using GCELibrary.Current;
using ZF.Model;
using System.Transactions;
using ZF.BLL.ServiceModel;

namespace ZF.WS.service
{
    public partial class fsywinfo : BLL.Page.PageBase
    {
        [PageArgument(Key = "id", CanNull = false)]
        public int Id { get; set; }
        public Model.Sys_Service Service { get; set; }
        public FSYW Model { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var service = dc.Sys_Service.FirstOrDefault(p => p.ServiceId == Id);
                if (service == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                this.Service = service;
                this.Model = (FSYW)GetJsonObj(service.Json);
                var member = SiteInfo.GetCurrentMember();

               
            }
        }

 
        public void InitControls()
        {
            
        }
        public object GetJsonObj(string json)
        {
            FSYW result = null;
            try
            {
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<FSYW>(json);
            }
            catch (Exception ex)
            {
                result = new FSYW();
            }
            return result;
        }
        public void Buy(object sender, EventArgs e)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var member = SiteInfo.GetCurrentMember();
                if (member == null)
                {
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                         DoPage = "/login.aspx?from="+this.Request.Url.ToString(),
                        Message = "请登录后操作",
                    }).Alert();
                    return;
                }


                var service = dc.Sys_Service.FirstOrDefault(p => p.ServiceId == Id);
                if (service == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                this.Service = service;
                this.Model = (FSYW)GetJsonObj(service.Json);

                if (this.Model.BuyNum >= 5)
                {
                    WarnFactory.CreateWarn(new PageWarnParameter()
                    {
                        JscriptMode = true,
                        Link = WarnLink.GoBack,
                        Message = "该业务已超过购买限额",
                    }).Alert();
                    return;
                }


                if (this.Model.BuyMemberIds!=null && !this.Model.BuyMemberIds.Contains(member.MemberId))
                {
                    this.Model.BuyMemberIds.Add(member.MemberId);
                    service.Json = Newtonsoft.Json.JsonConvert.SerializeObject(this.Model);

                    var success=SiteInfo.IntegralLog(member.MemberId, "购买风水委托业务信息",Model.Intre);

                    if (success)
                    {
                        dc.SubmitChanges();
                    
                    }
                    else
                    {
                        WarnFactory.CreateWarn(new PageWarnParameter()
                        {
                            JscriptMode = true,
                            Link = WarnLink.GoBack,
                            Message = "积分不足以该操作",
                        }).Alert();
                        return;
                    }


                    
                    
                }

                
            }
        }
    }
}
